import React, { Component } from 'react'
import styles from './style.less'

let week = ['日', '一', '二', '三', '四', '五', '六'];
let formatDate = (time, format) => {
    var date = new Date(time);
    var year = date.getFullYear(),
        month = date.getMonth() + 1,
        day = date.getDate(),
        hour = date.getHours(),
        min = date.getMinutes(),
        sec = date.getSeconds();
    var preArr = Array.apply(null, Array(10)).map((_, index) => {
        return '0' + index;
    });
    var newTime = format
        .replace(/YY/g, year)
        .replace(/MM/g, preArr[month] || month)
        .replace(/DD/g, preArr[day] || day)
        .replace(/dd/g, week[date.getDay()])
        .replace(/hh/g, preArr[hour] || hour)
        .replace(/mm/g, preArr[min] || min)
        .replace(/ss/g, preArr[sec] || sec);
    return newTime;
};

export default class index extends Component {
    constructor(props) {
        super(props);
        this.state = {
          nowTime: formatDate(new Date(), 'MM月DD日 星期dd hh:mm:ss'),
        };
        this.timer = setInterval(() => {
            this.setState({ nowTime: formatDate(new Date(), 'MM月DD日 星期dd hh:mm:ss') });
        }, 1000);
      }
      componentWillUnmount() {
        clearInterval(this.timer);
      }
    render() {
        return (
            <div className={styles.div_left1}>
                {this.state.nowTime}
            </div>
        )
    }
}
